package com.middlegroup.pay;

import com.middlegroup.pay.bind.SessionMerchantCodeArgumentResolver;
import com.middlegroup.pay.bind.SessionMerchantIdArgumentResolver;
import com.middlegroup.pay.bind.SessionSubjectCodeArgumentResolver;
import com.middlegroup.pay.servlet.ApiGatewayServlet;
import com.middlegroup.pay.servlet.SubjectGatewayServlet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @author pc
 * @date 2019/1/4
 */
@SpringBootApplication
@ComponentScan("com.middlegroup")
@MapperScan("com.middlegroup.pay.mapper")
public class PayApplication extends WebMvcConfigurationSupport {
    public static void main(String[] args) {
        SpringApplication.run(PayApplication.class, args);
    }


    /**一定要加，不然这个方法不会运行,一定要返回ServletRegistrationBean
     * @return
     */
    @Bean
    public ServletRegistrationBean getApiGatewayServletRegistrationBean() {
        //放入自己的Servlet对象实例
        ServletRegistrationBean bean = new ServletRegistrationBean(new ApiGatewayServlet());
        //访问路径值
        bean.addUrlMappings("/gateway");
        return bean;
    }

    /**一定要加，不然这个方法不会运行,一定要返回ServletRegistrationBean
     * @return
     */
    @Bean
    public ServletRegistrationBean getSubjectNotifyGatewayServletRegistrationBean() {
        //放入自己的Servlet对象实例
        ServletRegistrationBean subjectGatewayBean = new ServletRegistrationBean(new SubjectGatewayServlet());
        subjectGatewayBean.addUrlMappings("/notify");
        return subjectGatewayBean;
    }


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(new SessionMerchantCodeArgumentResolver());
        argumentResolvers.add(new SessionMerchantIdArgumentResolver());
        argumentResolvers.add(new SessionSubjectCodeArgumentResolver());
    }
}
